﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Xml.Linq;

namespace WebMassage
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            Application.Add("online", 0);
            Application.Add("access", 0);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();
            Application["online"] = (int)Application["online"] + 1;
            Application["access"] = (int)Application["access"] + 1;
            Application.UnLock();
            XElement xe = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Statistic.xml");
            xe.Element("Access").Value = (Convert.ToInt64(xe.Element("Access").Value) + 1) + "";
            xe.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Statistic.xml");
        }

        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            Application["online"] = (int)Application["online"] - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {
            Application.Remove("online");
            Application.Remove("access");
        }
    }
}